package com.tmall.tmall.controller;

import com.tmall.tmall.pojo.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/file")
public class CommonController {
    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile file) throws IOException {
        String img = Base64.getEncoder().encodeToString(file.getBytes());
        String originalFilename = file.getOriginalFilename();
        int idx = originalFilename.lastIndexOf('.');
        String extname = originalFilename.substring(idx+1);
        String newName = "data:image/"+extname+";base64,"+img;
        return Result.success(newName);
    }
}
